//
//  NotesByDateViewController2.m
//  Assignment3
//
//  Created by volsen on 3/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NotesByDateViewController2.h"
#import "Assignment3AppDelegate.h"

// Constant added by Vince on 3/26

#define sectionCount 1
#define dateSection 0




@implementation NotesByDateViewController2

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
// Next two lines added by Vince on 3/26
	
/*	noteDates = [[NSMutableArray alloc]
	initWithObjects:@"April 1, 2011",@"May 1, 2011",@"May 2, 2011",@"May 2, 2011",@"May 3, 2011",nil];	*/
	
	// loads all the notes
	noteDates = [[(Assignment3AppDelegate *)[[UIApplication sharedApplication] delegate] allNotes] retain];
	
	// sort all the notes by their dates
	for(int i = 0 ; i < [noteDates count] - 1 ; i++)
		for(int j = i + 1 ; j < [noteDates count] ; j++)
			if([[[noteDates objectAtIndex:i] objectForKey:@"date"] compare:[[noteDates objectAtIndex:j] objectForKey:@"date"]] == NSOrderedDescending){
				[noteDates exchangeObjectAtIndex:i withObjectAtIndex:j];
			}
	
//	[tableView setEditing:YES];
}

// Next method added by Vince on 3/26

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return sectionCount;
}

// Next method added by Vince on 3/26

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	if (section == dateSection)
//		return 8;  // Use for real app [dateSection count]
		return [noteDates count];
	else
		return 0;
}

// Next method added by Vince on 3/26

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	if (section == dateSection)
		return @"NOTE DATE";
	else
		return @"ERROR";
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

// Method added by Vince on 3/28

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if(cell == nil){
//		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}
	
	if(indexPath.section == dateSection){
//		[[cell textLabel] setText:[noteDates objectAtIndex:indexPath.row]];
		
		[[cell textLabel] setText:[[noteDates objectAtIndex:indexPath.row] objectForKey:@"title"]];
		
		NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
		[formatter setDateStyle:NSDateFormatterShortStyle];
		[formatter setTimeStyle:NSDateFormatterNoStyle];
		
		[[cell detailTextLabel] setText:[formatter stringFromDate:[[noteDates objectAtIndex:indexPath.row] objectForKey:@"date"]]];
		
		[formatter release];
	}
	else {
		[[cell textLabel] setText:@"Unknown"];
	}
	
	return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
	return YES;
}

- (BOOL)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
	[noteDates removeObjectAtIndex:indexPath.row];
	
	[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
	
	return YES;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//	NSLog(@"!");
	
	[[[[UIAlertView alloc] initWithTitle:[[noteDates objectAtIndex:indexPath.row] objectForKey:@"title"] message:[[noteDates objectAtIndex:indexPath.row] objectForKey:@"note"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];
}



- (void)dealloc {
	// Vince added next line on 3/26
	[noteDates release];
    [super dealloc];
}


@end
